#ifdef WX_PRECOMP
#include "wx_pch.h"
#else
#include <wx/wx.h>
#endif //WX_PRECOMP

#include "base_writer.h"
#include <wx/xml/xml.h>

#include "utility/xml_utility.h"

BaseWriter::BaseWriter()
  : Current(0)
{
  BeginDocument(wxEmptyString);
}

BaseWriter::~BaseWriter()
{
}

void BaseWriter::BeginDocument(const wxString& name)
{
  Data.reset(new wxXmlDocument());
  Current = new wxXmlNode(wxXML_ELEMENT_NODE, name);
  Data->SetRoot(Current);
}

void BaseWriter::BeginSection(const wxString& name)
{
  wxXmlNode* section = new wxXmlNode(wxXML_ELEMENT_NODE, name);
  XmlUtility::Append(Current, section);
  Current = section;
}

void BaseWriter::EndSection()
{
  wxXmlNode* parent = Current->GetParent();
  if (parent)
  {
    Current = parent;
  }
  else
  {
    wxASSERT_MSG(0, wxT("Don't syncronized Begin/EndSection"));
  }
}

void BaseWriter::Add(const wxString& name, const wxString& value)
{
  XmlUtility::Append(Current, XmlUtility::CreateContentNode(wxXML_ELEMENT_NODE, name, value));
}

void BaseWriter::Add(const wxString& name, int value)
{
  wxString sValue;
  sValue << value;
  Add(name, sValue);
}

wxXmlNode* BaseWriter::GetCurrent()
{
  return Current;
}

bool BaseWriter::Save(const wxString& filename, int indentstep) const
{
  return Data->Save(filename, indentstep);
}
